<?php

/**
 * Class IndexController
 */
class IndexController extends BaseController
{

    /**
    --------------------------------------注册登录-------------------------------------
     */

    /**
     * 登录
     */
    public function Login(){

        return $this->view('index.login');
    }

    /**
     * 登录验证
     * @return mixed
     */
    public function VerifyLogin()
    {
        if (Request::isMethod('POST')) {
            #接受参数
            $paramObj                   =          new stdClass();
            $paramObj->username         =          $this->getParam('username');        //用户名或手机号
            $paramObj->password         =          $this->getParam('password');         //密码
            $paramObj->remember         =          $this->getParam('remember');         //密码


            #逻辑处理
            try{
                IndexService::instance()->VerifyLogin($paramObj);
                if(!empty( $paramObj->remember)){
                    setcookie('name',$paramObj->username,time()+604800);
                }
                return Redirect::to('/');
            }catch (Exception $e){
                print $e->getMessage();
            }
        }
    }

    /**
     * js登陆验证
     * @return mixed
     */
    public function JsVerifyLogin()
    {
        if (Request::isMethod('POST')) {
            #接收参数
            $paramObj = new stdClass();
            $paramObj->username     =   $this->getParam('username');                 //用户名
            $paramObj->pwd          =   $this->getParam('pwd');                      //密码


            #逻辑处理
            if(!empty($paramObj->username)&&empty($paramObj->pwd))
            {
                $data = IndexService::instance()->JsCheckUser($paramObj);
                $rs = empty($data)?0:1;
                return $rs;
            }

            if(!empty($paramObj->pwd))
            {
                $data = IndexService::instance()->JsCheckPwd($paramObj);
                $rs = empty($data)?0:1;
                return $rs;
            }

        }
    }

    /**
     * 退出登录
     * @return mixed
     */
    public function LoginOut()
    {
        try{
            #清空SESSION
            Session::flush();
            return Redirect::to('/');
        }catch(Exception $e){
            $this->rest->error($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 注册
     */
    public function Register(){
        return $this->view('index.register');
    }

    /**
     * 注册验证
     * @return mixed
     */
    public function VerifyRegister()
    {

        if (Request::isMethod('POST')) {
            #接收参数
            $paramObj = new stdClass();
            $paramObj->user_name          =       $this->getParam('user_name');                 //用户名
            $paramObj->pwd                =       $this->getParam('pwd');                       //密码
            $paramObj->confirm_pwd        =       $this->getParam('confirm_pwd');               //确认密码
            $paramObj->mobile             =       $this->getParam('mobile');                    //联系方式
            $paramObj->captcha            =       $this->getParam('captcha');                   //验证码
            $paramObj->news_captcha       =       $this->getParam('news_captcha');              //短信验证码
            $paramObj->invite_mobile      =       $this->getParam('invite_mobile');             //邀请人手机号
            $paramObj->avatar             =       $this->getParam('avatar');                    //默认头像
            $paramObj->created            =       time();                                       //注册时间

            #逻辑处理
            try{
               IndexService::instance()->VerifyRegister($paramObj);
                echo "<script>alert('注册成功');location.href='/register_success'</script>";
            }catch(Exception $e){
                print_R($e->getMessage());
                //$this->rest->error($e->getMessage(), $e->getCode());
            }
        }
    }

    /**
     * js注册验证
     * @return int
     */
    public function JsVerifyRegister()
    {
        if (Request::isMethod('POST')) {
            #接收参数
            $paramObj = new stdClass();
            $paramObj->username          =       $this->getParam('username');                 //用户名
            $paramObj->mobile            =       $this->getParam('mobile');                   //联系方式
            $paramObj->invite_mobile     =       $this->getParam('invite_mobile');            //邀请人电话
            $paramObj->news_captcha      =       $this->getParam('news_captcha');             //短信验证码
            $paramObj->captcha           =       $this->getParam('captcha');                  //验证码

            #逻辑处理
            if(!empty($paramObj->captcha))
            {
                $data = IndexService::instance()->JsCheckCaptcha($paramObj);
                return $data;
            }
            if(!empty($paramObj->news_captcha))
            {
                $data = IndexService::instance()->JsCheckNews($paramObj);
                return $data;
            }
            if(!empty($paramObj->username))
            {
                $data = IndexService::instance()->JsCheckUserName($paramObj);
                $rs = empty($data)?0:1;
                return $rs;
            }
            if(!empty($paramObj->mobile))
            {
                $data = IndexService::instance()->JsCheckMobile($paramObj);
                $rs = empty($data)?0:1;
                return $rs;
            }
            if(!empty($paramObj->invite_mobile))
            {
                $data = IndexService::instance()->JsCheckInvite($paramObj);
                $rs = empty($data)?0:1;
                return $rs;
            }

        }
    }

    /**
     * 短信验证
     * @return string
     */
    public function VerifyNews()
    {
        if(Request::isMethod('POST')){
            #接收参数
            $paramObj = new stdClass();
            $paramObj->mobile            =       $this->getParam('mobile');                  //联系方式

            if(!empty($paramObj->mobile))
            {
                $data = IndexService::instance()->VerifyNews($paramObj);
                return $data;
            }
        }
    }

    /**
     * 生成验证码
     */
    public function Captcha()
    {
        CaptchaService::instance()->create();
    }

    /**
     * 注册成功跳转
     * @return mixed
     */
    public function RegisterSuccess()
    {
        return $this->view('index.register_success');
    }

    /**
     * 找回密码
     * @return mixed
     */
    public function UserRetrievePwdOne()
    {
        return $this->view('index.user_retrieve_pwd_one');
    }

    /**
     * 忘记密码手机验证
     */
    public function JsVerifyUserMobile()
    {
        if(Request::isMethod('POST')){
            #接收参数
            $paramObj = new stdClass();
            $paramObj->mobile             =       $this->getParam('mobile');                 //电话
            $paramObj->captcha            =       $this->getParam('captcha');                //验证码


            if(!empty($paramObj->captcha))
            {
                $data = IndexService::instance()->JsVerifyUserCaptcha($paramObj);
                if($data == 2){
                    return $data;
                }else if(!empty($paramObj->mobile)){
                    $data2 = IndexService::instance()->JsVerifyUserMobile($paramObj);
                    $rs = empty($data2)?0:1;
                    return $rs;
                }
            }
        }
    }

    /**
     * 忘记密码手机验证
     * @return mixed
     */
    public function VerifyUserMobile()
    {
        if(Request::isMethod('POST')){
            #接收参数
            $paramObj = new stdClass();
            $paramObj->mobile       = $this->getParam('mobile');                 //电话
            $paramObj->captcha      = $this->getParam('captcha');                //验证码

            #逻辑处理
            try{
                $rs = IndexService::instance()->VerifyUserMobile($paramObj);
                if($rs){
                    $data = IndexService::instance()->VerifyNews($paramObj);
                    return Redirect::to('/user_retrieve_pwd_two');
                }
            }catch(Exception $e){
                print_R($e->getMessage());
                //$this->rest->error($e->getMessage(), $e->getCode());
            }

        }
    }
    public function UserRetrievePwdTwo()
    {
        return $this->view('index.user_retrieve_pwd_two');
    }

    public function JsVerifyUserNews()
    {
        if(Request::isMethod('POST')){
            #接收参数
            $paramObj = new stdClass();
            $paramObj->news_captcha            =       $this->getParam('news_captcha');              //短信验证

            if(!empty($paramObj->news_captcha))
            {
                $data = IndexService::instance()->VerifyNews($paramObj);
                return $data;
            }
        }
    }

    public function UserRetrievePwdThr()
    {
        return $this->view('index.user_retrieve_pwd_thr');
    }
    public function UserRetrievePwdFou()
    {
        return $this->view('index.user_retrieve_pwd_fou');
    }

    public function ProjectPay()
    {
        return $this->view('index.project_pay');
    }

    public function UserMoneyInstant()
    {
        return $this->view('index.user_money_instant');
    }
    /**
    --------------------------------------注册登录结束-------------------------------------
     */

    public function index()
    {
        //获取央票通列表
        $ypt_info = IndexService::instance()->YptProjectList();
        //获取上市通列表
        $sst_info = IndexService::instance()->SstProjectlist();
        //获取新闻列表
        $news_info = IndexService::instance()->NewsIndexList();
        //公告列表
        $announcement = IndexService::instance()->AnnouncementList();
        $num_project = IndexService::instance()->ProjectNum();//贴现项目总数
        $all_project_scale = IndexService::instance()->ProjectScale(); //项目总额度
        $all_invest_record = IndexService::instance()->AllInvestRecord();  //投资人总收益
        $all_invest_num = IndexService::instance()->AllInvestNum(); //成交笔数
        $index_all = array('num_project'=>$num_project,'all_project_scale'=>$all_project_scale,'all_invest_record'=>$all_invest_record,'all_invest_num'=>$all_invest_num,);
        return $this->view('index.index')->with(['ypt'=>$ypt_info,'sst'=>$sst_info,'news'=>$news_info,'announcement'=>$announcement,'index_all'=>$index_all]);
    }

    /**
     * ypt,sst详情
     * $type ypt 1 sst 2
     */
    public function Project(){
        $project_id = $this->getParam('project_id');
        $type       = $this->getParam('type');
        //获取央票通列表
        $info = IndexService::instance()->YptProjectList($project_id);
        //获取企业信息
        $page_all = IndexService::instance()->InvestRecordListPage($project_id,1);
        if($type==1){
            return $this->view('index.project_details_ypt')->with(['ypt'=>$info,'page_all'=>$page_all]);
        }else{
            return $this->view('index.project_details_sst')->with('sst',$info);
        }

    }

    /**
     * ajax分页
     */
    public function InvestList(){
        $project_id = $this->getParam('project_id');
        $page = $this->getParam('page');
        $pagelist = IndexService::instance()->InvestRecordListPage($project_id,$page);
        return $this->rest->success(['state' => $pagelist ]);
    }




    /**
     * 贴现流程
     * @return mixed
     */
    public function TopFlow()
    {
        return $this->view('index.top_flow');
    }

    /**
     * 出票央企
     * @return mixed
     */
    public function TopEnterYpt()
    {
        return $this->view('index.top_enterprises_ypt');
    }

    /**
     * 上市通
     * @return mixed
     */
    public function TopEnterSst()
    {
        return $this->view('index.top_enterprises_sst');
    }

    /**
     * 文章页
     * @return mixed
     */
    public function TopEnterArticle()
    {
        return $this->view('index.top_enterprises_article');
    }

    /**
     * 票据理财
     */
    public function ProjectList(){
        return $this->view('index.project_list');
    }

    /**
     * 持票融资
     */
    public function Financing(){
        return $this->view('index.financing');
    }

    /**
     * 关于我们
     */
    public function AboutIntro(){
        return $this->view('index.aboutus_introduction');
    }

    /**
     * 关于我们-发展历程
     */
    public function AboutHistory(){
        return $this->view('index.aboutus_history');
    }

    /**
     * 关于我们-团队介绍
     */
    public function AboutTeam(){
        return $this->view('index.aboutus_team');
    }

    /**
     * 关于我们-合作伙伴
     */
    public function AboutPartner(){
        return $this->view('index.aboutus_partner');
    }

    /**
     * 关于我们-资讯中心
     */
    public function AboutInfo(){
        return $this->view('index.aboutus_information');
    }

    /**
     * 关于我们-加入我们
     */
    public function AboutJoinUs(){
        return $this->view('index.aboutus_joinus');
    }

    /**
     * 关于我们-联系我们
     */
    public function AboutContactUs(){
        return $this->view('index.aboutus_contactus');
    }

    /**
     * 帮助中心
     */
    public function FooterHelp(){
        return $this->view('index.footer_help');
    }

    /**
     * 安全保障
     */
    public function FooterInsurance(){
        return $this->view('index.footer_insurance');
    }

    /**
     * 投资建议
     */
    public function FooterProposals(){
        return $this->view('index.footer_proposals');
    }

    public function ProjectSuccess()
    {
        return $this->view('index.project_success');
    }

}